package com.dudu.controller;

import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;

import com.dudu.bean.AccountBean;
import com.dudu.bean.AppExtendBean;
import com.dudu.service.IAccountExtendService;
import com.dudu.service.IAppExtendService;
import com.dudu.service.IOperationPlatformCustAccountService;
import com.dudu.service.IOperationPlatformService;
import com.dudu.service.impl.AccountExtendServiceImpl;
import com.dudu.service.impl.AppExtendServiceImpl;
import com.dudu.service.impl.OperationPlatformCustAccountServiceImpl;
import com.dudu.service.impl.OperationPlatformServiceImpl;

import core.CoreService;
/**
 * H5 调用运营平台接口
 * @author zx
 *
 */
public class BusinessPlatformController extends CoreService implements IBusinessPlatformController {
	IOperationPlatformCustAccountService operationPlatformCustService=new OperationPlatformCustAccountServiceImpl();
	IOperationPlatformService operationPlatformService = new OperationPlatformServiceImpl();
	/**
	 * 查询个人注册用户限额
	 */
	@Override
	public String queryOauthRelationUser() {
		String callback = this.getRequest().getParameter("callback");
		String periphery_code = this.getRequest().getParameter("periphery_code");
		String periphery_account = this.getRequest().getParameter("periphery_account");
		String periphery_user_account = this.getRequest().getParameter("periphery_user_account");
		JSONObject resultData =operationPlatformCustService.queryOauthRelationUser(periphery_code, periphery_account, periphery_user_account);
		if (!StringUtils.isEmpty(callback)) {
			return returnData(callback + "(" + resultData.toString() + ")");
		} else {
			return returnData(resultData.toString());
		}
	}

	/**
	 * 查询用户是否还有余额
	 */
	@Override
	public String hasArrearage() {
		HttpServletRequest request = this.getRequest();
		
		IAppExtendService appExtendService = new AppExtendServiceImpl();
		IAccountExtendService accountExtendService = new AccountExtendServiceImpl();
		AppExtendBean appExtendBean = appExtendService.queryAppExtendByIdentify(request.getParameter("app_key"));
		AccountBean accountBean = accountExtendService
				.queryAccountDetailByCustAccount(request.getParameter("cust_account"));
		boolean useable = operationPlatformService.platformAuthentication(new JSONObject(), appExtendBean, accountBean,
				request.getParameter("abilityType"));
		JSONObject resultData = new JSONObject();
		if (useable) {
			resultData.put("result", "0");
		} else {
			resultData.put("result", "1");
		}
		String callback = request.getParameter("callback");
		if (!StringUtils.isEmpty(callback)) {
			return returnData(callback + "(" + resultData.toString() + ")");
		} else {
			return returnData(resultData.toString());
		}
	}

	/**
	 * 查询用户资源及余额情况
	 */
	@Override
	public String queryResourcesAndAccountInfo() {
		String cust_account = this.getRequest().getParameter("cust_account");
		String app_key = this.getRequest().getParameter("app_key");
		JSONObject object=operationPlatformCustService.queryResourcesAndAccountInfo(cust_account, app_key, null);
		String callback = this.getRequest().getParameter("callback");
		if (!StringUtils.isEmpty(callback)) {
			return returnData(callback + "(" + object.toString() + ")");
		} else {
			return returnData(object.toString());
		}
	}

	/**
	 * 获取请求码
	 */
	@Override
	public String getAskCode() {
		String osCode = this.getRequest().getParameter("periphery_code");
		String apiCode = this.getRequest().getParameter("api_code");
		JSONObject object=operationPlatformCustService.getAskCode(osCode, apiCode);
		return returnData(object.toString());
	}
  
}
